package com.yupi.yudada.score;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yupi.yudada.mapper.QuestionMapper;
import com.yupi.yudada.mapper.ScoreResultMapper;
import com.yupi.yudada.model.dto.question.QuestionContentDTO;
import com.yupi.yudada.model.entity.App;
import com.yupi.yudada.model.entity.Question;
import com.yupi.yudada.model.entity.ScoreResult;
import com.yupi.yudada.model.entity.UserAnswer;
import com.yupi.yudada.model.vo.QuestionVO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @Author ortonzhu
 * @Date 2024/11/16 17:09
 */
@Component
public class CustomTestScoreStrategy implements ScoreStrategy {
    @Resource
    private QuestionMapper questionMapper;
    @Resource
    private ScoreResultMapper scoreResultMapper;

    @Override
    public UserAnswer calculateScore(List<String> choice, App app) {
        // 1. 根据id获取题目和 题目结果信息
        Question question = questionMapper.selectOne(Wrappers.<Question>lambdaQuery().eq(Question::getAppId, app.getId()));
        List<ScoreResult> scoreResults = scoreResultMapper.selectList(Wrappers.<ScoreResult>lambdaQuery().eq(ScoreResult::getAppId, app.getId()));
        //2.计用户每个选择对应的属性个数，如I=10个，E=5个
        QuestionVO questionVO = QuestionVO.objToVo(question);
        HashMap<String, Integer> optionCount = new HashMap<>();
        List<QuestionContentDTO> questionContentDTO = questionVO.getQuestionContent();
        for (QuestionContentDTO contentDTO : questionContentDTO) {
            for (String answer : choice) {
                for (QuestionContentDTO.Option option : contentDTO.getOptions()) {
                    if (answer.equals(option.getKey())) {
                        String result = option.getResult();
                        if (!optionCount.containsKey(result)) {
                            optionCount.put(result, 0);
                        } else {
                            optionCount.put(result, optionCount.get(result) + 1);
                        }
                    }

                }
            }
        }
        // 遍历每种评分结果，计算哪个结果的得分更高
        int maxScore = 0;
        ScoreResult maxScoreResult = scoreResults.get(0);
        for (ScoreResult scoreResult : scoreResults) {
            List<String> resultProp = JSONUtil.toList(scoreResult.getResultProp(), String.class);
            int score = resultProp.stream().mapToInt(item -> optionCount.getOrDefault(item, 0)).sum();
            if (score > maxScore) {
                maxScore = score;
                maxScoreResult = scoreResult;
            }
        }
        //4. 造返回值，填充答案对象的属性
        UserAnswer userAnswer = new UserAnswer();
        userAnswer.setAppId(app.getId());
        userAnswer.setAppType(app.getAppType());
        userAnswer.setScoringStrategy(app.getScoringStrategy());
        userAnswer.setChoices(JSONUtil.toJsonStr(choice));
        userAnswer.setResultId(maxScoreResult.getId());
        userAnswer.setResultName(maxScoreResult.getResultName());
        userAnswer.setResultDesc(maxScoreResult.getResultDesc());
        userAnswer.setResultPicture(maxScoreResult.getResultPicture());
        return userAnswer;
    }
}
